package cn.yuanqiao.archive.report.service.impl;/**
 * @author 赵常飞
 * @date 2023/8/9 14:54
 */

import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.report.mapper.reportMapper;
import cn.yuanqiao.archive.report.service.reportService;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 *
 *@AUTHOR:赵常飞
 *@date 2023/8/9 14:54
 */
@Service
public class reportServiceImpl implements reportService {
    @Autowired
    private reportMapper reportmapper;
    @Autowired
    private ITenantSysConfigService sysConfigService;

    @Value("${jmreport.url}")
    private String jmreportUrl;
    @Override
    public Map geturl(JSONObject jsonObject) {
        Long menuId=jsonObject.getLong("menuId");
        Long categoryId=jsonObject.getLong("categoryId");
        String type=jsonObject.getString("type").toLowerCase(Locale.ROOT);
        String tenantcode= SecurityUtils.getTenantCode();
        JSONArray data=jsonObject.getJSONArray("data");
        JSONArray datalist = jsonObject.getJSONArray("datalist");

        String modelname="";
        //不是档案数据,静态打印等
        if(null==categoryId||"".equals(categoryId)||"null".equals(categoryId)){
            String name=jsonObject.getString("name");
            modelname=menuId+"_"+type.toUpperCase()+"_"+name;
        }else{
            modelname=menuId+"_"+categoryId+"_"+type;
        }
        //修改对应的json数据
        if(datalist.size()==1){
            reportmapper.updatejson(modelname,tenantcode, JSON.toJSONString((JSONObject)data.get(0), SerializerFeature.WriteMapNullValue));
        }else{
            for (int i = 0; i < datalist.size(); i++) {
                reportmapper.updatejsonbyname(modelname,tenantcode,String.valueOf(data.get(i)),String.valueOf(datalist.get(i)));
            }
        }

        String url=sysConfigService.selectConfigByKey("REPORT_URL");;
        String modelid=reportmapper.getmodelid(tenantcode,modelname);
        Map returnmap=new HashMap(1<<2);
        if("".equals(modelid)||null==modelid){
            returnmap.put("result","fail");
            returnmap.put("msg","未找到相应模板,请先配置报表模板:"+modelname);
        }else{
            returnmap.put("result","success");
            returnmap.put("modelname",modelname);
            returnmap.put("url",url+"/view/"+modelid);
        }
        return returnmap;
    }

    @Override
    public String getreportindex() throws UnsupportedEncodingException {
        String url=jmreportUrl;//sysConfigService.selectConfigByKey("REPORT_URL");
        String tenantcode= SecurityUtils.getTenantCode();
        LoginUser user = SecurityUtils.getLoginUser();
        String username=user.getUsername();
        String userid=String.valueOf(user.getUserId());
        JSONObject json=new JSONObject();
        json.put("UserID",userid);
        json.put("UserName",username);
        json.put("TenantCode",tenantcode);
        byte[] byteArray = json.toJSONString().getBytes("GBK");
        byte[] encodedBytes = Base64.getEncoder().encode(byteArray);
        String base64String = new String(encodedBytes);
        return url+"/jmreport/list?json="+base64String;
    }
}
